﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack))
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = WebConfigurationManager.ConnectionStrings["baglanti"].ToString();
            cn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select Birim_Adi from Birimler", cn);
            da.Fill(ds, "Table");
            drplistBirimler.DataSource = ds.Tables[0];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                drplistBirimler.Items.Add(new ListItem(ds.Tables[0].Rows[i]["Birim_Adi"].ToString()));
            }
            cn.Close();
        }
    }
    protected void btnYukle_Click(object sender, EventArgs e)
    {        
        if (FileUpload1.HasFile)
        {
            lblBilgi.Text = "Yüklenen Resim";
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = WebConfigurationManager.ConnectionStrings["baglanti"].ToString();
            cn.Open();
            FileUpload1.SaveAs(Server.MapPath("image\\") + FileUpload1.FileName);

            Literal lt = new Literal();
            lt.Text = "<img src='image\\" + FileUpload1.FileName + "' class='style18' Height='460px' Width='997px'/>";
            pnlResim.Controls.Add(lt);

            Kayitlar NKayit = new Kayitlar();
            SqlCommand cm = new SqlCommand("Select Birim_ID from Birimler Where Birim_Adi ='" + drplistBirimler.SelectedValue + "'", cn);
            SqlDataReader dr = cm.ExecuteReader();
            if (dr.Read())
            {
                NKayit.Birim_ID = Convert.ToInt32(dr["Birim_ID"]);
            }
            else
                return;
            NKayit.Kat_No = drplistKatlar.SelectedValue;
            NKayit.Oda_No = txtOdalar.Text;
            NKayit.ImagePath = FileUpload1.FileName;
            db.Insert("Kayitlar", NKayit, false);
            cn.Close();
        }
    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        SqlConnection cn=new SqlConnection();
        cn.ConnectionString = WebConfigurationManager.ConnectionStrings["baglanti"].ToString();
        cn.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select Oda_No from Kayitlar Where Oda_No Like '" + prefixText + "%'", cn);
        da.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["Oda_No"].ToString(), i);
            i++;
        }
        cn.Close();
        return items;        
    }
}